package com.dc.chat.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dc.chat.app.biz.BackBiz;
import com.dc.chat.app.service.mp.SensitiveService;
import com.dc.chat.app.service.mp.UserService;
import com.dc.chat.entity.dao.CommonMessage;
import com.dc.chat.entity.dao.FeedBack;
import com.dc.chat.entity.dao.Sensitive;
import com.dc.chat.entity.dao.User;
import com.dc.chat.entity.to.ResponseBean;
import com.dc.chat.entity.to.back.AddSensitiveReq;
import com.dc.chat.entity.to.back.GetEditUserListReq;
import com.dc.chat.entity.to.back.LoginReq;
import com.dc.chat.interceptor.security.Admin;
import com.dc.chat.interceptor.security.Security;
import com.mysql.cj.jdbc.result.ResultSetImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/back")
public class BackController {
    @Resource
    UserService userService;
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    BackBiz backBiz;
    @PostMapping("/login")
    public ResponseBean<String> login(@RequestBody LoginReq loginReq){
        return backBiz.login(loginReq.getUsername(),loginReq.getPassword());
    }
    @Admin
    @PostMapping("/getOnlineNum")
    public ResponseBean<Integer> getOnlineNum(){
        return backBiz.getOnlineNum();
    }
    @Admin
    @PostMapping("/getEditUserList")
    public ResponseBean<List<User>> getEditUserList(){
        return backBiz.getEditUserList();
    }
    @Admin
    @PostMapping("/getFeedBackList")
    public ResponseBean<List<FeedBack>> getFeedBack(){
        return backBiz.getFeedBack();
    }
    @Admin
    @PostMapping("/getCommonMessage")
    public ResponseBean<List<CommonMessage>> getCommonMessage(){
        return backBiz.getCommonMessage();
    }
    @Admin
    @PostMapping("/removeCommonMessage")
    public ResponseBean removeCommonMessage(@RequestParam("id") Integer id){
        return backBiz.removeCommonMessage(id);
    }
    @Admin
    @PostMapping("/markFeedBack")
    public ResponseBean markFeedBack(@RequestParam("id") Integer id){
        return backBiz.markFeedBack(id);
    }
    @Admin
    @PostMapping("/deleteFeedBack")
    public ResponseBean deleteFeedBack(@RequestParam("id") Integer id){
        return backBiz.deleteFeedBack(id);
    }
    @Admin
    @PostMapping("/disableUser")
    public ResponseBean disableUser(@RequestParam("userid") Integer userid){

        User byId = userService.getById(userid);
        disonline(byId.getUserName());
        byId.setStatus(1);
        userService.updateById(byId);
        //告诉所有管理员 重新获取列表
        Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
        Map map=new HashMap();
        map.put("refresh",true);
        for (String adminToken : adminTokens) {
            simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
        }
        return ResponseBean.success("操作成功",null);
    }
    @Admin
    @PostMapping("/enableUser")
    public ResponseBean enableUser(@RequestParam("userid") Integer userid){
        User byId = userService.getById(userid);
        byId.setStatus(0);
        userService.updateById(byId);
        //告诉所有管理员 重新获取列表
        Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
        Map map=new HashMap();
        map.put("refresh",true);
        for (String adminToken : adminTokens) {
            simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
        }
        return ResponseBean.success("操作成功",null);
    }
    @Admin
    @PostMapping("/disonline")
    public ResponseBean disonline (@RequestParam("username") String username){

        Map userTokenMap = redisTemplate.opsForHash().entries("login");
        String toekn = (String) userTokenMap.get(username);
        simpMessagingTemplate.convertAndSend("/u/"+toekn,"fuck");
        //告诉所有管理员 重新获取列表
        Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
        Map map=new HashMap();
        map.put("refresh",true);
        for (String adminToken : adminTokens) {
            simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
        }
        return ResponseBean.success("操作成功",null);
    }
    @Admin
    @PostMapping("/addSensitive")
    public void addSensitive(@RequestBody AddSensitiveReq req){
        backBiz.addSensitive(req);
    }
    @Resource
    SensitiveService.SensitiveMapper sensitiveMapper;
    @Admin
    @PostMapping("/getSensitive")
    public ResponseBean getSensitive(){
        List<Sensitive> sensitives = sensitiveMapper.selectList(Wrappers.lambdaQuery(new Sensitive()).orderByDesc(Sensitive::getCreateTime));
        return ResponseBean.success("",sensitives);
    }
    @Admin
    @PostMapping("/delSensitive")
    public void delSensitive(@RequestParam("id") Integer id){
        int i = sensitiveMapper.deleteById(id);
        if(i==1){
            Map map=new HashMap();
            map.put("refreshSensitive",true);
            Set<String> adminToken = redisTemplate.opsForSet().members("admin");
            for (String token : adminToken) {
                simpMessagingTemplate.convertAndSend("/back/"+token,"",map);
            }
        }
    }
}
